package star.cloud.asme.asmePerson.asmePersonExamContent;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;
import star.cloud.asme.base.utils.ObjectsUtil;

@Service
@Slf4j
@RequiredArgsConstructor
public class AsmePersonExamContentServiceImpl extends IBaseServiceImpl<AsmePersonExamContentMapper, AsmePersonExamContent> implements AsmePersonExamContentService {

    private final AsmePersonExamContentMapper asmePersonExamContentMapper;

    @Override
    public Result<AsmePersonExamContent> getOneByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<AsmePersonExamContent> queryWrapper = new LambdaQueryWrapper<>();
        if (columnName.equals("id")) {
            queryWrapper.eq(AsmePersonExamContent::getId, columnValue);
        }
        AsmePersonExamContent asmePersonTrainCourse = asmePersonExamContentMapper.selectOne(queryWrapper);
        return Result.ok(asmePersonTrainCourse);
    }

    @Override
    public Result<IPage<AsmePersonExamContent>> getListByConditionsWithPage(AsmePersonExamContent conditions) {
        IPage<AsmePersonExamContent> iPage = new Page<>(conditions.getCurrentPage(), conditions.getPageSize());
        LambdaQueryWrapper<AsmePersonExamContent> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ObjectsUtil.noNullAndEmpty(conditions.getMethod()), AsmePersonExamContent::getMethod, conditions.getMethod());
        queryWrapper.eq(ObjectsUtil.noNullAndEmpty(conditions.getLevel()), AsmePersonExamContent::getLevel, conditions.getLevel());
        queryWrapper.eq(ObjectsUtil.noNullAndEmpty(conditions.getExamKind()), AsmePersonExamContent::getExamKind, conditions.getExamKind());

        queryWrapper.orderByAsc(AsmePersonExamContent::getMethod);
        queryWrapper.orderByAsc(AsmePersonExamContent::getExamKind);
        queryWrapper.orderByAsc(AsmePersonExamContent::getSequence);
        asmePersonExamContentMapper.selectPage(iPage, queryWrapper);
        return Result.ok(iPage);
    }

    @Override
    public Result<AsmePersonExamContent> insertOne(AsmePersonExamContent entity) {
        Integer maxSequence = asmePersonExamContentMapper.getMaxSequence(entity);
        if (maxSequence == null) {
            entity.setSequence(1);
        } else {
            entity.setSequence(++maxSequence);
        }
        int inserted = asmePersonExamContentMapper.insert(entity);
        setCreatorAndUpdater(entity);
        return inserted > 0 ? Result.ok(entity) : Result.error();
    }

    @Override
    public Result<AsmePersonExamContent> updateOne(AsmePersonExamContent entity) {
        setVersion(entity);
        int updated = asmePersonExamContentMapper.updateById(entity);
        setCreatorAndUpdater(entity);
        return updated > 0 ? Result.ok(entity) : Result.error();
    }
}
